package directory.model;

import controller.ParametroMgr;

import directory.controller.PersonTypeAttributeMgr;
import html.HtmlForm;

import java.io.Serializable;
import model.BasicBean;

import java.sql.ResultSet;
import java.util.Vector;


public class Person extends BasicBean implements Serializable {
    public String idPersonType = "";
    public String nit = "";
    public String nombre = "";
    public String direccion = "";
    public String telefono = "";
    public String celular = "";
    public String email = "";
    public String idMunicipio = "11001";

    public Person() {
    }

    public Person(ResultSet rs) {
        try {
            id = validateString(rs.getString("id"));
            idPersonType = validateString(rs.getString("idPersonType"));
            nit = validateString(rs.getString("nit"));
            nombre = validateString(rs.getString("nombre"));
            direccion = validateString(rs.getString("direccion"));
            telefono = validateString(rs.getString("telefono"));
            celular = validateString(rs.getString("celular"));
            email = validateString(rs.getString("email"));
            idMunicipio = validateString(rs.getString("idMunicipio"));
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    @Override
    public String getHtmlRow() {
        String sz = "";
        sz += ("<td class='st4middle'>" + nit + "</td>");
        sz += ("<td class='st4middle'>" + nombre + "</td>");
        sz += ("<td class='st4middle'>" + direccion + "</td>");
        sz += ("<td class='st4middle'>" + telefono + "</td>");
        sz += ("<td class='st4middle'>" + celular + "</td>");
        sz += ("<td class='st4middle'>" + email + "</td>");
        sz += ("<td class='st4middle'>" +
        ParametroMgr.mgr.getItem("Municipio", idMunicipio).nombre + "</td>");

        return sz;
    }

    public HtmlForm getHtmlForm(String title, boolean editable) {
        HtmlForm form = new HtmlForm("FormPersona"+id, title, "_self",
                "", "600");
        form.setEditable(editable);

        form.addHidden("id:", id);
        form.addSQText("Nombre:", "nombre", nombre, "10");
        form.addSQText("Dirección:", "direccion", direccion, "10");
        form.addSQText("Teléfono:", "telefono", telefono, "10");
        form.addSQText("Celular:", "celular", celular, "10");
        form.addSQText("Email:", "email", email, "10");

        Vector<PersonTypeAttribute> lstExt = PersonTypeAttributeMgr.mgr.getList(idPersonType);

        for (PersonTypeAttribute it : lstExt) {
            String val = ParametroMgr.mgr.getItemFromSql(
                    "select id, value from PersonAttributeValue where idPerson =" +
                    id + " and idPersonTypeAttribute=" + it.id).nombre;
            int tipo = Integer.parseInt(it.idTipoAtributo);

            switch (tipo) {
            case 1:
            case 2:
            case 4:
            case 5:
                form.addSQText(it.nombre, "PPE_" + it.id, val, "30");
                break;

            case 3:
                form.addSQParametroDrop(it.nombre, "PPE_" + it.id, val,
                    it.propiedades);

                break;

            case 6:
                form.addSQDate(it.nombre, "PPE_" + it.id, val);
                break;

            case 7:
                form.addSQPersonBrowser(it.nombre, "PPE_" + it.id, val,
                    "select * from nombre where idPersonType=1");
                break;

            case 8:
                form.addSQDeptoMpioBrowser(it.nombre, "PPE_" + it.id, "11001");
                break;
            }
        }

        return form;
    }

    @Override
    public String getUpdateSql() {
        String sql = "";

        if (id.equals("")) {
            sql = "insert into Person( idPersonType, nit, nombre, direccion, telefono, celular, email, idMunicipio ) values( '" +
                idPersonType + "', '" + nit + "', '" + nombre + "', '" +
                direccion + "', '" + telefono + "', '" + celular + "', '" +
                email + "', '" + idMunicipio + "' )";
        } else {
            sql = "update Person set idPersonType = '" + idPersonType +
                "', nit= '" + nit + "', nombre= '" + nombre +
                "', direccion= '" + direccion + "', telefono= '" + telefono +
                "', celular= '" + celular + "', email= '" + email +
                "', idMunicipio= '" + idMunicipio + "' where id = '" + id +
                "'";
        }

        return sql;
    }

    @Override
    public String getIdentifier() {
        return "idPerson=" + id;
    }
}
